package com.efei.jdbc.sql;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SQL的参数值
 */
@Setter
@Getter
public class SqlParameter {

    private String name;
    private Object value;
    private SqlParameterType sqlParameterType;

    SqlParameter(String name, Object value) {
        this.name = name;
        this.value = value;
    }

    SqlParameter(String name, Object value, String parmeterType) {
        this.name = name;
        this.value = value;
        this.sqlParameterType = SqlParameterType.getByKey(parmeterType);
    }

    public static SqlParameter of(String name, Object value) {
        return new SqlParameter(name,value);
    }

    public static SqlParameter of(String name, Object value, String parmeterType) {
        return new SqlParameter(name,value,parmeterType);
    }


    public Object getValue() {
        if(sqlParameterType ==null || value==null) {
            return value;
        }
        return tranValue(value, sqlParameterType);
    }

    private Object tranValue(Object value, SqlParameterType type) {
        Object newValue = value;
        if (sqlParameterType.INT.equals(type)) {
            if(value instanceof String) {
                newValue = Integer.parseInt((String)value);
            }
        } else if (sqlParameterType.LONG.equals(type)) {
            if(value instanceof String) {
                newValue = Long.parseLong((String)value);
            }
        } /*else if (PARMETER_TYPE_DATE.equals(type)) {
            if(value instanceof String) {
                newValue = DateUtil.parseDate((String)value, DATE_FORMAT);
            }
        } else if (PARMETER_TYPE_TIME.equals(type)) {
            if(value instanceof String) {
                newValue = DateUtil.parseDate((String)value, TIME_FORMAT);
            }
        }*/ else if(sqlParameterType.STRING.equals(type)) {
            return value.toString();
        }
        return newValue;
    }

    public static List<SqlParameter> of(int size) {
        List<SqlParameter> parameters = new ArrayList<SqlParameter>(size);
        return parameters;
    }

    public static List<SqlParameter> of() {
        List<SqlParameter> parameters = new ArrayList<SqlParameter>();
        return parameters;
    }
}
